/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.dao.ActivityGoodsSpecDao;
import com.jeesite.modules.storage.dao.OrderGoodsDao;
import com.jeesite.modules.storage.entity.OrderGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 订单商品Service
 */
@Service
@Transactional(readOnly=true)
public class OrderGoodsService extends CrudService<OrderGoodsDao, OrderGoods> {

    @Autowired
    private ActivityGoodsSpecDao activityGoodsSpecDao;

    /**
     * 保存
     * @param memberId 会员Id
     * @param orderId 订单Id
     * @param cartId 购物车Id
     * @param transActivityId 转发Id
     * @param goodsSpecId  商品规格Id
     * @param num 数量
     * @param remark 备注
     * @return
     */
    @Transactional(readOnly=false)
    public void save(String transMemberId,String memberId, String transActivityId,String orderId, String cartId, String goodsSpecId,Double salePrice,Double agentPrice, Integer num, String remark)
    {
        OrderGoods orderGoods = new OrderGoods();
        orderGoods.setTransMemberId(transMemberId);
        orderGoods.setMemberId(memberId);
        orderGoods.setTransActivityId(transActivityId);
        orderGoods.setOrderId(orderId);
        orderGoods.setCartId(cartId);
        orderGoods.setGoodsSpecId(goodsSpecId);

        Map map = activityGoodsSpecDao.findById(goodsSpecId);
        orderGoods.setActivityId(String.valueOf(map.get("activityId")));
        orderGoods.setBrandId(String.valueOf(map.get("brandId")));
        orderGoods.setCateId(String.valueOf(map.get("cateId")));
        orderGoods.setGoodsId(String.valueOf(map.get("goodsId")));
        orderGoods.setGoodsSpecId(String.valueOf(map.get("goodsSpecId")));

        orderGoods.setSourcePrice(Double.parseDouble(String.valueOf(map.get("price"))));
        orderGoods.setSalePrice(salePrice);
        orderGoods.setAgentPrice(agentPrice);

        orderGoods.setNum(num);
        orderGoods.setAmount(salePrice * num);
        orderGoods.setRemark(remark);
        super.save(orderGoods);
    }
}